package com.heihei.uncompression;

import com.heihei.script.DeComUtil;
import com.heihei.utils.FileUtilsF;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

/**
 *  生成解压文件的脚本
 *
 * @version 1.0
 * @since JDK1.8
 * @author duanhengfei
 * @date 2021年01月27日 16:04:46
 */
public class DeComDemo {
    //数组存储支持的文件类型
    private static String[] coms = {"7Z", "ZIP", "RAR", "GZIP", "BZIP2", "TAR", "CAB", "ISO", "ARJ", "LZH", "CHM", "WIM", "CPIO", "RPM", "DEB", "NSIS"};

    public static void main(String[] args) throws IOException {
        //System.out.println(geneAllScript());
        System.out.println(">>>解压文件<<<");
        String scriptPath = DeComUtil.getScriptPath();
        //解决 scriptPath 覆盖的问题
        File scriptFile = getValidScriptFile(scriptPath);
        String name = scriptFile.getName();
        String fileNameWithoutExt = FileUtilsF.removeExtension(name);
        String extensionName = FileUtilsF.getExtensionName(name);
        int i = 0;
        while(scriptFile.exists()) {
            scriptFile = new File(scriptFile.getParentFile(), fileNameWithoutExt + i + "." + extensionName);
            i++;
        }


        FileUtilsF.writeTxtFile(geneAllScript(), scriptFile, "GBK");
        System.out.println("生成脚本成功，脚本位置： " + scriptPath);
    }

    /**
     * 方法描述: 获取有效的scriptPath
     *
     * @param scriptPath
     * @Return {@link File}
     * @throws
     * @author duanhengfei
     * @date 2021年01月27日 16:05:44
     */
    public static File getValidScriptFile(String scriptPath) {
        File validScriptFile = new File(scriptPath);
        if(!validScriptFile.exists()) {
            return validScriptFile;
        }
        int index = 0;
        while(validScriptFile.exists()) {
            index ++;
            validScriptFile = new File(scriptPath + index + ".bat");
        }
        DeComUtil.setScriptPath(validScriptFile.getAbsolutePath());
        return validScriptFile;
    }


    //生成整体脚本字符串
    public static String geneAllScript() throws IOException {
        StringBuilder sb = new StringBuilder();

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入源文件目录");
        //源目录
        File validSrcPath = FileUtilsF.getValidSrcPath();

        //密码
        String password = null;

        System.out.println("是否有密码？");
        String inputStr = sc.nextLine().trim();

        if("Y".equalsIgnoreCase(inputStr) || "YES".equalsIgnoreCase(inputStr)) {
            //有密码
            System.out.println("请输入密码");
            password = sc.nextLine();
        }

        final String endPassword = password;

        System.out.println("是否解压到当前文件夹？");
        inputStr = sc.nextLine().trim();
        //如果在此处输入Y，则不嵌套；否则，则嵌套
        System.out.println("是否取消嵌套文件夹？");
        String inputNest = sc.nextLine().trim();
        if("Y".equalsIgnoreCase(inputStr) || "YES".equalsIgnoreCase(inputStr)) {
            //解压到当前文件夹
            FileUtilsF.handleFiles(validSrcPath, (file) -> {
                String name = file.getName().toLowerCase();
                if(Arrays.asList(coms).contains(FileUtilsF.getExtensionName(name).toUpperCase())) {
                    sb.append(DeComUtil.generateLine(file, endPassword, ("Y".equalsIgnoreCase(inputNest) || "YES".equalsIgnoreCase(inputNest)) ? false : true));
                }
            });

        } else {
            //需要指定目录
            System.out.println("请输入目标文件夹");
            final File validDesDir = FileUtilsF.getValidDesDir();
            FileUtilsF.handleFiles(validSrcPath, (file) -> {
                String name = file.getName().toLowerCase();
                if(Arrays.asList(coms).contains(FileUtilsF.getExtensionName(name).toUpperCase())) {
                    //多截取出来的路径
                    String substring = file.getAbsolutePath().substring(validSrcPath.getAbsolutePath().length());
                    File newValidDesDir = new File(validDesDir, substring);
                    if("Y".equalsIgnoreCase(inputNest) || "YES".equalsIgnoreCase(inputNest)) {
                        sb.append(DeComUtil.generateLine(file, newValidDesDir.getParentFile(), endPassword));
                    } else {
                        sb.append(DeComUtil.generateLine(file, new File(newValidDesDir.getParent(), FileUtilsF.removeExtension(name)), endPassword));
                    }
                }
            });
        }

        sb.append("pause");
        return sb.toString();
    }
}
